<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动id" prop="classifyId">
        <el-input
          v-model="queryParams.classifyId"
          placeholder="请输入活动id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:classify:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="classifyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="ID" align="center" prop="classifyId" />
      <el-table-column label="活动类型名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '1'">正常</span>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 添加或修改活动类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动id" prop="classifyId">
          <el-input v-model="form.classifyId" placeholder="请输入活动id" />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入类型名称" />
        </el-form-item>
      </el-form>

      <el-button
        style="margin-bottom:10px;"
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd1"
      >新增</el-button>
      <el-table
        :data="fieldList"
        border
        style="width: 100%">
        <el-table-column
          prop="fieldName"
          label="字段名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="filedLabel"
          label="字段label"
          width="180">
        </el-table-column>
        <el-table-column
          prop="fieldType"
          label="字段类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.fieldType == '1'">多行文本</el-tag>
            <el-tag v-if="scope.row.fieldType == '2'">下拉单选</el-tag>
            <el-tag v-if="scope.row.fieldType == '3'">下拉多选</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="dictValues"
          label="下拉"
          width="180">
        </el-table-column>
        <el-table-column
          prop="seq"
          label="序号">
        </el-table-column>
        <el-table-column
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="handleUpdate1(scope.row)" size="small">编辑</el-button>
            <el-button type="text" @click="handleDelete1(scope.row)" size="small">删除</el-button>
            
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改活动类型对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="80px">
        <el-form-item label="字段名称" prop="classifyId">
          <el-input v-model="form1.fieldName" placeholder="请输入活动id" />
        </el-form-item>
        <el-form-item label="字段label" prop="name">
          <el-input v-model="form1.filedLabel" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="字段类型" prop="name">
          <el-select v-model="form1.fieldType" placeholder="字段类型" style="width:100%;">
            <el-option
              v-for="dict in fieldTypeList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item v-if="form1.fieldType == 2 || form1.fieldType == 3" label="下拉项" prop="dictValues">
          <el-tag
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">新增</el-button>        
        </el-form-item>



         <el-form-item label="序号" prop="name">
          <el-input v-model="form1.seq" placeholder="请输入类型名称" />
        </el-form-item>
      </el-form>
       <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClassify, getClassify, delClassify, addClassify, updateClassify } from "@/api/system/classify";

export default {
  name: "Classify",
  data() {
    return {
      fieldList: [],
      fieldTypeList: [
        {
          id: '1',
          name: '多行文本'
        },
        {
          id: '2',
          name: '下拉单选'
        },
        {
          id: '3',
          name: '下拉多选'
        },
      ],
      fieldId: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动类型表格数据
      classifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classifyId: null,
        name: null,
        status: null,
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
      },


      open1: false,
      form1: {},
      title1: '',
      rules1: {},

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      dynamicTags: [],
      inputVisible: false,
      inputValue: ''
    };
  },
  created() {
    this.fieldId = 1
    this.getList();
  },
  methods: {
    // 多个tag 开始
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    // 多个tag 结束

    /** 查询活动类型列表 */
    getList() {
      this.loading = true;
      listClassify(this.queryParams).then(response => {
        this.classifyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classifyId: null,
        name: null,
        status: "0",
        isDel: null,
        version: null,
        createUserId: null,
        createUserName: null,
        createTime: null,
        updateUserId: null,
        updateUserName: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    reset1() {
     this.form1 = {
        id: null,
        fieldName: null,
        filedLabel: null,
        fieldType: null,
        seq: null,
        dictValues: null
      }
      this.resetForm("form1");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动类型";
    },

    handleAdd1() {
      this.dynamicTags = []
      this.reset1()
      // this.form1.id = this.fieldId++
      this.open1 = true;
      this.title1 = "添加字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClassify(id).then(response => {
        this.form = response.data;
        this.fieldList = response.data.activityClassifyFieldList

        // 赋最大值
        let id = 1
        this.fieldList.map(item => {
          if(item.id >= id) {
            id = item.id
          }
        })
        this.fieldId = ++id

        

        this.open = true;
        this.title = "修改活动类型";
      });
    },
    handleUpdate1(row) {
      this.reset1();
      this.form1 = row;

      if (this.form1.dictValues) {
        this.dynamicTags = this.form1.dictValues.split(';')
        this.inputVisible = false;
        this.inputValue = '';
      }else {
        this.dynamicTags = []
      }


      this.open1 = true;
      this.title1 = "修改字段";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.activityClassifyFieldList = this.fieldList
          if (this.form.id != null) {
            // let a = ''
            // this.dynamicTags.map(item => {
            //     a += item + ';'
            // })
            // if(a) {
            //   a = a.substr(0,a.length - 1)
            // }
            // this.form.dictValues = a


            updateClassify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // let a = ''
            // this.dynamicTags.map(item => {
            //     a += item + ';'
            // })
            //  if(a) {
            //   a = a.substr(0,a.length - 1)
            // }
            // this.form.dictValues = a

            addClassify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm1() {
      this.$refs["form1"].validate(valid => {
        if(this.form1.id) {
          this.fieldList = this.fieldList.map(item => {
            if(item.id == this.form1.id) {
              let a = ''
              this.dynamicTags.map(item => {
                  a += item + ';'
              })
              if(a) {
                a = a.substr(0,a.length - 1)
              }
              item.dictValues = a

              item.fieldName = this.form1.fieldName
              item.filedLabel = this.form1.filedLabel
              item.fieldType = this.form1.fieldType
              item.seq = this.form1.seq
            }
            return item
          })
        }else {
          this.form1.id = this.fieldId++

          let a = ''
          this.dynamicTags.map(item => {
              a += item + ';'
          })
          if(a) {
            a = a.substr(0,a.length - 1)
          }
          this.form1.dictValues = a

          this.fieldList.push(this.form1)
        }
        this.open1 = false;
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动类型编号为"' + ids + '"的数据项？').then(function() {
        return delClassify(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    handleDelete1(row) {
      this.fieldList = this.fieldList.filter(item => item.id != row.id)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/classify/export', {
        ...this.queryParams
      }, `classify_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
